const app = getApp();
let tool = require('../../../utils/tool.js');
Page({
    data: {
        imageurl: 'https://yxpmaterial.oss-cn-beijing.aliyuncs.com/',
        year: 0,
        month: 0,
        week: ['日', '一', '二', '三', '四', '五', '六'],
        dateArr: [],
        isToday: 0,
        isTodayWeek: false,
        todayIndex: 0,
        confirmles: [], //待确认
        confirmed: [], //已确认
        justsub: [],
        classList: [],
        paramDate: 0 //选定的日期
    },
    onLoad: function(option) {
        this.setData({
            paramDate: option.seldate
        });
        this.nowdata(parseInt(option.month), parseInt(option.year));
        this.getData();
        this.getList();
    },
    nowdata: function(months, years) {
        this.processDataList();
        let now = new Date();
        let year = 0;
        let month = 0
        if (!months) {
            year = now.getFullYear();
            month = now.getMonth() + 1;
            this.dateInit();
        } else {
            year = years;
            month = months;
            this.dateInit(parseInt(years), parseInt(months) - 1);
        }
        this.setData({
            year: year,
            month: month,
            isToday: '' + now.getFullYear() + ((parseInt(now.getMonth()) + 1).toString()) + now.getDate()
        })
    },
    dateInit: function(setYear, setMonth) {
        //全部时间的月份都是按0~11基准，显示月份才+1
        let dateArr = []; //需要遍历的日历数组数据
        let arrLen = 0; //dateArr的数组长度
        let now = setYear ? new Date(setYear, setMonth) : new Date();
        let year = setYear || now.getFullYear();
        let nextYear = 0;
        let month = setMonth || now.getMonth(); //没有+1方便后面计算当月总天数
        let nextMonth = (month + 1) > 11 ? 1 : (month + 1);
        let startWeek = new Date(year + '/' + (month + 1) + '/' + 1).getDay(); //目标月1号对应的星期
        let dayNums = new Date(year, nextMonth, 0).getDate(); //获取目标月有多少天
        let obj = {};
        let num = 0;
        //设置timepicker的年份范围
        this.setData({
            startDate: year,
            endDate: year + 1
        });
        if (month + 1 > 11) {
            nextYear = year + 1;
            dayNums = new Date(nextYear, nextMonth, 0).getDate();
        }
        arrLen = startWeek + dayNums;
        for (let i = 0; i < arrLen; i++) {
            if (i >= startWeek) {
                num = i - startWeek + 1;
                obj = {
                    isToday: '' + year + (month + 1) + num,
                    formateDate: '' + year + '/' + (month > month ? month + 1 : '0' + (month + 1)) + '/' + (num < 10 ? '0' + num : num),
                    dateNum: num,
                    weight: 5
                }
            } else {
                obj = {};
            }
            dateArr[i] = obj;
        }
        this.setData({
            dateArr: dateArr
        })
        let nowDate = new Date();
        let nowYear = nowDate.getFullYear();
        let nowMonth = nowDate.getMonth() + 1;
        let nowWeek = nowDate.getDay();
        let getYear = setYear || nowYear;
        let getMonth = setMonth >= 0 ? (setMonth + 1) : nowMonth;

        if (nowYear == getYear && nowMonth == getMonth) {
            this.setData({
                isTodayWeek: true,
                todayIndex: nowWeek
            })
        } else {
            this.setData({
                isTodayWeek: false,
                todayIndex: -1
            })
        }
    },
    lastMonth: function() {
        //全部时间的月份都是按0~11基准，显示月份才+1
        let year = this.data.month - 2 < 0 ? this.data.year - 1 : this.data.year;
        let month = this.data.month - 2 < 0 ? 11 : this.data.month - 2;
        this.setData({
            year: year,
            month: (month + 1),
            classList: new Array()
        })
        //获取每个月的每日的约课
        this.dateInit(year, month);
    },
    nextMonth: function() {
        //全部时间的月份都是按0~11基准，显示月份才+1
        let year = this.data.month > 11 ? this.data.year + 1 : this.data.year;
        let month = this.data.month > 11 ? 0 : this.data.month;
        this.setData({
            year: year,
            month: (month + 1),
            classList: new Array()
        })
        this.dateInit(year, month);
    },
    cutclass: function(e) {
        let that = this;
        let index = e.currentTarget.dataset.yindex;
        let id = e.currentTarget.dataset.id;
        let temp = this.data.classList;
        //删除课程
        let paramArr = {
            access_token: app.globalData.studentInfo.access_token,
            id: id
        }
        let rs = tool.getSign(paramArr, app.globalData.key); //返回 bodyParam heaer sign
        let sign = rs.sign;
        let apiUrl = app.globalData.apiURL + "user/delschedule?sign=" + sign;
        let deletePro = new Promise(
            function(resolve, reject) {
                wx.request({
                    url: apiUrl,
                    header: rs.header,
                    data: rs.bodyParam,
                    method: 'post',
                    success(res) {
                        resolve(res);
                    }
                })
            }
        );
        deletePro.then(function(res) {
            if (res.data.code === 1) {
                wx.showToast({
                    title: res.data.info,
                    icon: "success",
                    duration: 2000

                });
                temp.splice(index, 1); //start time ,end time
                that.setData({
                    classList: temp
                });
            } else if (res.data.code == 10001) {
                wx.redirectTo({
                    url: '/pages/login/login',
                })
            } else {
                wx.showToast({
                    title: res.data.info,
                    duration: 2000
                });
            }
            that.getData();
        });
    },
    processDataList: function() { //处理约课列表的相关数据
        let cl = this.data.classList;

        this.setData({
            classList: cl
        })
    },
    toDateClass: function() {
        if (getCurrentPages()[getCurrentPages().length - 2].route == "pages/dateClass/dateClass?seldate="+this.data.paramDate) {
            //navigatorback
            wx.navigateBack();
        } else {
            wx.navigateTo({
              url: '../dateClass?seldate='+this.data.paramDate,
            })
        }
    },
    getData: function() {
        let that = this;
        if (!this.data.selectDate) {
            let now = new Date();
            let year = now.getFullYear();
            let month = now.getMonth() + 1;
            this.data.selectDate = year.toString() + '-' + (month > 9 ? month.toString() : "0" + month.toString());
        } else {
            let year = this.data.year;
            let month = this.data.month;
            this.data.selectDate = year.toString() + '-' + (month > 9 ? month.toString() : "0" + month.toString());
        }
        let paramArr = {
            access_token: app.globalData.studentInfo.access_token,
            date: this.data.selectDate
        }
        let rs = tool.getSign(paramArr, app.globalData.key); //返回 bodyParam heaer sign
        let sign = rs.sign;
        let apiUrl = app.globalData.apiURL + "user/historynote?sign=" + sign;
        let classPro = new Promise(
            function(resolve, reject) {
                wx.request({
                    url: apiUrl,
                    header: rs.header,
                    data: rs.bodyParam,
                    method: 'post',
                    success(res) {
                        resolve(res);
                    }
                })
            }
        );
        let classHis = new Object();
        classPro.then(function(res) {
            if (res.data.code === 1) {
                classHis = res.data;
                that.setData({
                    confirmed: classHis.data.confirmed,
                    confirmles: classHis.data.confirmles,
                    justsub: classHis.data.rowfirm
                });
            } else if (res.data.code == 10001) {
                wx.redirectTo({
                    url: '/pages/login/login',
                })
            }
        })
    },
    getList: function(e) {
        let listHis = new Object();
        let that = this;
        if (typeof(e) !== 'undefined') {
            let selDate = e.currentTarget.dataset.fordate;
            this.setData({
                paramDate: selDate
            });
        }
        let paramArr = {
            access_token: app.globalData.studentInfo.access_token,
            date: this.data.paramDate
        }
        let rs = tool.getSign(paramArr, app.globalData.key); //返回 bodyParam heaer sign
        let sign = rs.sign;
        let apiUrl = app.globalData.apiURL + "user/history?sign=" + sign;
        let listPro = new Promise(
            function(resolve, reject) {
                wx.request({
                    url: apiUrl,
                    header: rs.header,
                    data: rs.bodyParam,
                    method: 'post',
                    success(res) {
                        resolve(res);
                    }
                })
            }
        );
        listPro.then(function(res) {
            if (res.data.code == 1) {
                listHis = res.data;
                for (let ele in listHis.data) {
                    listHis.data[ele].date = listHis.data[ele].start_time.slice(0, 10);
                    listHis.data[ele].start_time = listHis.data[ele].start_time.slice(10);
                    listHis.data[ele].end_time = listHis.data[ele].end_time.slice(10);
                    switch (parseInt(listHis.data[ele].status)) {
                        case 0:
                            {
                                listHis.data[ele].img = "yueke_icon_daiqueren@2x.png";
                                listHis.data[ele].dis = "none";
                                break;
                            }
                        case 1:
                            {
                                listHis.data[ele].img = "yueke_icon_yiqueren@2x.png";
                                listHis.data[ele].dis = "none";
                                break;
                            }
                        default:
                            {
                                listHis.data[ele].img = "yueke_icon_yitijiao@2x.png";
                                listHis.data[ele].dis = "block";
                            }
                    }
                }
                that.setData({
                    classList: listHis.data
                })
            } else if (res.data.code == 10001) {
                wx.redirectTo({
                    url: '/pages/login/login',
                })
            } else {
                that.setData({
                    classList: {}
                })
                wx.showToast({
                    title: res.data.info,
                    icon: "none"
                })
            }
        })
    }
})